#ifndef KEY_H
#define KEY_H

#include "common_func.h"
#include <stdint.h>
#include <sm2.h>

#define KEY_BASEADDR 		0xbf100400

#define KEY_TYPE 			KEY_BASEADDR
#define KEY_INDEX 			(KEY_BASEADDR + 0x04)
#define KEY_PIN_0 			(KEY_BASEADDR + 0x08)			
#define KEY_PIN_1 			(KEY_BASEADDR + 0x0c)
#define KEY_1_IN_0 			(KEY_BASEADDR + 0x10)
#define KEY_1_IN_1 			(KEY_BASEADDR + 0x14)
#define KEY_1_IN_2 			(KEY_BASEADDR + 0x18)
#define KEY_1_IN_3 			(KEY_BASEADDR + 0x1c)
#define KEY_1_IN_4 			(KEY_BASEADDR + 0x20)
#define KEY_1_IN_5 			(KEY_BASEADDR + 0x24)
#define KEY_1_IN_6 			(KEY_BASEADDR + 0x28)
#define KEY_1_IN_7 			(KEY_BASEADDR + 0x2c)
#define KEY_2_IN_0 			(KEY_BASEADDR + 0x30)
#define KEY_2_IN_1 			(KEY_BASEADDR + 0x34)
#define KEY_2_IN_2 			(KEY_BASEADDR + 0x38)
#define KEY_2_IN_3 			(KEY_BASEADDR + 0x3c)
#define KEY_2_IN_4 			(KEY_BASEADDR + 0x40)
#define KEY_2_IN_5 			(KEY_BASEADDR + 0x44)
#define KEY_2_IN_6 			(KEY_BASEADDR + 0x48)
#define KEY_2_IN_7 			(KEY_BASEADDR + 0x4c)
#define KEY_3_IN_0 			(KEY_BASEADDR + 0xa0)
#define KEY_3_IN_1 			(KEY_BASEADDR + 0xa4)
#define KEY_3_IN_2 			(KEY_BASEADDR + 0xa8)
#define KEY_3_IN_3 			(KEY_BASEADDR + 0xac)
#define KEY_3_IN_4 			(KEY_BASEADDR + 0xb0)
#define KEY_3_IN_5 			(KEY_BASEADDR + 0xb4)
#define KEY_3_IN_6 			(KEY_BASEADDR + 0xb8)
#define KEY_3_IN_7 			(KEY_BASEADDR + 0xbc)
#define KEY_1_OUT_0 		(KEY_BASEADDR + 0x50)
#define KEY_1_OUT_1 		(KEY_BASEADDR + 0x54)
#define KEY_1_OUT_2 		(KEY_BASEADDR + 0x58)
#define KEY_1_OUT_3 		(KEY_BASEADDR + 0x5c)
#define KEY_1_OUT_4 		(KEY_BASEADDR + 0x60)
#define KEY_1_OUT_5 		(KEY_BASEADDR + 0x64)
#define KEY_1_OUT_6 		(KEY_BASEADDR + 0x68)
#define KEY_1_OUT_7 		(KEY_BASEADDR + 0x6c)
#define KEY_2_OUT_0 		(KEY_BASEADDR + 0x70)
#define KEY_2_OUT_1 		(KEY_BASEADDR + 0x74)
#define KEY_2_OUT_2 		(KEY_BASEADDR + 0x78)
#define KEY_2_OUT_3 		(KEY_BASEADDR + 0x7c)
#define KEY_2_OUT_4 		(KEY_BASEADDR + 0x80)
#define KEY_2_OUT_5 		(KEY_BASEADDR + 0x84)
#define KEY_2_OUT_6 		(KEY_BASEADDR + 0x88)
#define KEY_2_OUT_7 		(KEY_BASEADDR + 0x8c)
#define KEY_3_OUT_0 		(KEY_BASEADDR + 0xc0)
#define KEY_3_OUT_1 		(KEY_BASEADDR + 0xc4)
#define KEY_3_OUT_2 		(KEY_BASEADDR + 0xc8)
#define KEY_3_OUT_3 		(KEY_BASEADDR + 0xcc)
#define KEY_3_OUT_4 		(KEY_BASEADDR + 0xd0)
#define KEY_3_OUT_5 		(KEY_BASEADDR + 0xd4)
#define KEY_3_OUT_6 		(KEY_BASEADDR + 0xd8)
#define KEY_3_OUT_7 		(KEY_BASEADDR + 0xdc)
#define KEY_READY 			(KEY_BASEADDR + 0x90)
#define KEY_STATE 			(KEY_BASEADDR + 0x94)
#define KEY_USE_INT_KEY		(KEY_BASEADDR + 0x98)
#define KEY_MODE			(KEY_BASEADDR + 0x9c)
#define KEY_IN_VALID		(KEY_BASEADDR + 0xe0)

#define KEY_TYPE_SM4		0x1
#define KEY_TYPE_SM2		0x0

#define KEY_MODE_DELETE		0x2		
#define KEY_MODE_READ		0x0
#define KEY_MODE_WRITE		0x1
#define KEY_MODE_RELEASE	0x3

#define KEY_WAIT_TIME		0x01

uint32_t write_key(uint32_t type, uint32_t index, uint8_t *pin, ECCrefPrivateKey *prikey, ECCrefPublicKey *pubkey, uint8_t *sm4_key);
uint32_t delete_key(uint32_t type, uint32_t index, uint8_t *pin);
uint32_t access_key_test(uint32_t type, uint32_t index, uint8_t *pin, ECCrefPrivateKey *prikey, ECCrefPublicKey *pubkey, uint8_t *sm4_key);
uint32_t access_key(uint32_t type, uint32_t index, uint8_t *pin);
uint32_t release_key(uint32_t type, uint32_t index);

#endif 
